package org.xuyuji.study.spring_hibernate_demo.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.xuyuji.study.spring_hibernate_demo.dao.UserDao;
import org.xuyuji.study.spring_hibernate_demo.entity.User;

@Repository("userDao")
@Transactional
public class UserDaoImpl extends BaseDao implements UserDao {

	public int insert(User user) {
		return Integer.parseInt(getHibernateTemplate().save(user).toString());
	}

	public void delete(int id) {
		getHibernateTemplate().delete(getHibernateTemplate().get(User.class, id));
	}

	public void update(User user) {
		getHibernateTemplate().update(user);
	}

	public User selectById(int id) {
		List<User> list = getHibernateTemplate().find("from User where id = ?", id);
		return list.size() == 0 ? null : list.get(0);
	}

	public List<User> selectAll() {
		return getHibernateTemplate().find("from User");
	}

}
